﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.GData.Extensions.MediaRss;
using yt = Google.YouTube;

public partial class results : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		string query = Request.QueryString["q"];
		if (string.IsNullOrEmpty(query))
			return;

		// http://gdata.youtube.com/feeds/videos/-/QUERY	(by tag)
		// http://gdata.youtube.com/feeds/videos?q=QUERY	(by search)

		int start = 0;
		if (Request.QueryString["start"] != null)
			int.TryParse(Request.QueryString["start"], out start);

		FeedQuery fq = new FeedQuery() {
			StartIndex = start,
			NumberToRetrieve = 50,
			Query = query };
		if (Request.QueryString["type"] != null)
			switch (Request.QueryString["type"].ToLower())
			{
				case "tag":			
				case "category":	fq.Uri = new Uri("http://gdata.youtube.com/feeds/videos/-/" + query); break;
				case "user":  fq.Uri = new Uri("http://gdata.youtube.com/feeds/videos?q=" + query); fq.Author = query;  break;
				case "query":
				default:  fq.Uri = new Uri("http://gdata.youtube.com/feeds/videos?q=" + query); fq.Query = query;  break;
			}
		query += "&v=2&max-results=25&key=" + System.Configuration.ConfigurationManager.AppSettings["GoogleApiKey"];
		// caption param
		if (Request.QueryString["caption"] != null)
			query += "&caption" + (bool.Parse(Request.QueryString["caption"])
				? "=true"
				: "=false");

		// max results
		int max = 10000;
		if (Request.QueryString["max"] != null)
			int.TryParse(Request.QueryString["max"], out max);


		AtomFeed feed = Common.Search.GetFeed(fq, max);
		IEnumerable<AtomEntry> result = feed.Entries.ToList().AsEnumerable();

		// Filter by date. AFTER we download the result set, because YouTube (A Google company) sucks at search.
		DateTime dstart, dstop;
		if (Request.QueryString["start"] != null)
		{
			DateTime.TryParse(Request.QueryString["start"], out dstart);
			result = result.Where(f => f.Published >= dstart);
			//fq.StartDate = dstart;
		}
		if (Request.QueryString["stop"] != null)
		{
			DateTime.TryParse(Request.QueryString["stop"], out dstop);
			result = result.Where(f => f.Published <= dstop);
			//fq.EndDate = dstop;
		}

		rResults.DataSource = result;
		rResults.DataBind();
    }
	protected void rResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
			return;
		AtomEntry entry = (AtomEntry)e.Item.DataItem;

		HyperLink hlTitle = (HyperLink)e.Item.FindControl("hlTitle");
		Label
			lblDescription = (Label)e.Item.FindControl("lblDescription"),
			lblDate = (Label)e.Item.FindControl("lblDate");

		hlTitle.Text = entry.Title.Text;
		hlTitle.NavigateUrl = entry.AlternateUri.ToString();
		lblDate.Text =
			"<a href=\"" + entry.Authors[0].Uri.ToString().Replace("gdata.", "www.").Replace("feeds/users/", "") + "\">" + entry.Authors[0].Name + "</a><br />" +
			entry.Published.ToString("MM/dd/yyyy");
		
		string desc = entry.Content.Content;
			desc = desc.Substring(desc.IndexOf("<span>") + 6);
			desc = desc.Substring(0, desc.IndexOf("</span>"));
			lblDescription.Text = desc;

		

		
		

	}
}